package com.kidedu.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;

import com.kidedu.dao.mapper.KClassMapper;
import com.kidedu.entity.KClass;
import com.kidedu.entity.KClassExample;
import com.kidedu.entity.dto.KClassDTO;
import com.kidedu.service.IKClassService;
import com.kidedu.util.BaseService;
import com.kidedu.util.Pagination;

public class KClassService extends BaseService implements IKClassService{
    
	@Autowired
	private KClassMapper kClassMapper;
	
	
	private int pageSize = 10;
	
	
	public int addKClass(KClass kClass) {
		KClassExample example = new KClassExample();
		example.setOrderByClause("id desc");
		List<KClass> KClassList = kClassMapper.selectByExample(example);
		int pre = 0;
		if(KClassList.size() > 0){
			pre = Integer.parseInt(KClassList.get(0).getId());
		}
		kClass.setId(String.valueOf(pre + 1));
		kClass.setState(false);
		kClass.setCreatetime(new Date());
		return kClassMapper.insert(kClass);
	}
	
	
	public int deleteKClass(String cId) {
		return kClassMapper.deleteByPrimaryKey(cId);
	}
	

	public int updateKClass(KClass kClass) {
		return kClassMapper.updateByPrimaryKey(kClass);
	}
	
	
	public List<KClass> selectKClassList() {
		KClassExample example = new KClassExample();
		example.setOrderByClause("createTime desc");
		return kClassMapper.selectByExample(example);
	}
	
	
	public Pagination<KClass> selectKClassList(int pageNo, String keyWords) {
		KClassExample example = new KClassExample();
		example.setOrderByClause("createTime desc");
		if(keyWords != null){
			keyWords = "%" + keyWords + "%";
			example.createCriteria().andNameLike(keyWords);
		}
		int totalCount = kClassMapper.countByExample(example);
		List<KClass> kClassList = kClassMapper.selectByExample(example, new RowBounds(super.calculateStartPage(pageNo, pageSize), pageSize));
		return new Pagination<KClass>(pageNo, pageSize, totalCount, kClassList);
	}
	
	
	/*某一课程大类下所有课程*/
	public List<KClassDTO> getClassList(String sort){
		KClassExample example = new KClassExample();
		example.createCriteria().andStateEqualTo(true).andNoteEqualTo(sort);
		List<KClass> clslist = kClassMapper.selectByExample(example);
		List<KClassDTO> classlist = new ArrayList<KClassDTO>();
		for(KClass kclass : clslist){
			KClassDTO classDTO = new KClassDTO(kclass);
			classlist.add(classDTO);
		}
		return classlist;
	}


	@Override
	public Pagination<KClass> selectKClassList(int pageSize, int PageNo) {
		// TODO Auto-generated method stub
		KClassExample example = new KClassExample();
		int totalCount = kClassMapper.countByExample(example);
		List<KClass> kClassList = kClassMapper.selectByExample(example, new RowBounds(super.calculateStartPage(PageNo, pageSize), pageSize));
		return new Pagination<KClass>(PageNo, pageSize, totalCount, kClassList);
	}


	@Override
	public Pagination<KClass> selectKClassListPass(int pageSize, int PageNo) {
		// TODO Auto-generated method stub
		KClassExample example = new KClassExample();
		example.createCriteria().andStateEqualTo(true);
		int totalCount = kClassMapper.countByExample(example);
		List<KClass> kClassList = kClassMapper.selectByExample(example, new RowBounds(super.calculateStartPage(PageNo, pageSize), pageSize));
		return new Pagination<KClass>(PageNo, pageSize, totalCount, kClassList);
	}
}